export default class extends think.Service {
    constructor(user) {
        super();
        const db = this.db;
        this.eventModel = db['cms_event'];
        this.attachmentModel = db['sys_attachment'];
        this.user = user;
    }

    /**
     * 违法抓拍上报事件
     * userid --上报用户
     * baidu_x --经度
     * baidu_y --纬度
     * source --事件来源
     * realpath --图片路径
     */
    async saveEventByVideo(data) {
        const eventData = {
            create_by: data.userid,
            update_by: data.userid,
            create_date: new Date(),
            update_date: new Date(),
            source: data.source,
            remarks: data.remarks,
            baidu_x: data.baidu_x,
            baidu_y: data.baidu_y,
            ObjPosition: data.ObjPosition,
            id: think.uuid('v1')
        };
        const attachmentData = {
            id: think.uuid('v1'),
            businesskey: eventData.id,
            realpath: data.realpath,
            create_date: eventData.create_date,
            user_id: data.userid
        };
        try {
            const event = await this
                .eventModel
                .create(eventData);
            const attachment = await this
                .attachmentModel
                .create(attachmentData);
            return JSON.parse(JSON.stringify(event));
        } catch (err) {
            return this
                .exceptionService
                .handleError(err);
        }
    }

    /**
     * 获取摄像头及目录列表
     * @returns {Promise<*>}
     */
    async getCatalogList() {
        const Res = await this
            .CrudService
            .findAll('device_catalog_video', {is_enable: 1});
        const defauType = Res.filter(item => {
            return item.type !== 2;
        });
        const type2 = Res.filter(item => {
            return item.type === 2;
        });
        for (let index = 0; index < type2.length; index++) {
            const sql = `SELECT * FROM device_catalog_video WHERE parent_ids LIKE '${type2[index].parent_ids + type2[index].device_id}%'`;
            const count = await this
                .CrudService
                .querySql(sql);
            type2[index].count = count.length;
        }
        return [
            ...defauType,
            ...type2
        ];
    }
}
